package com.lishe.lishe.Imageloader;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.lishe.lishe.lishe.base.BaseApplication;
import com.lishe.lishe.lishe.utils.Utils;

import java.security.MessageDigest;

/**
 * 标   题：
 * 功能描述：
 * 作   者：邹修新
 * 创建时间：2017/11/8.
 */

public class GlideRoundTransform extends BitmapTransformation {

    private static float cornerRadius = 0f;
    public static final int CORNER_TOP_LEFT = 1;
    public static final int CORNER_TOP_RIGHT = 1 << 1;
    public static final int CORNER_BOTTOM_LEFT = 1 << 2;
    public static final int CORNER_BOTTOM_RIGHT = 1 << 3;
    public static final int CORNER_TOP_LEFT_RIGHT = CORNER_TOP_LEFT | CORNER_TOP_RIGHT;
    public static final int CORNER_ALL = CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT;
    protected int corners = CORNER_ALL;

    public GlideRoundTransform() {
        this(Utils.dip2px(BaseApplication.getContext(), 5));
    }

    public GlideRoundTransform(int corners, int cornerRadius) {
        this(cornerRadius);
        this.cornerRadius = cornerRadius;
        this.corners = corners;
    }

    public GlideRoundTransform(int dp) {
        super();
        this.cornerRadius = dp;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
        int notRoundedCorners = corners ^ CORNER_ALL;
        //哪个角不是圆角我再把你用矩形画出来
        if ((notRoundedCorners & CORNER_TOP_LEFT) != 0) {
            canvas.drawRect(0, 0, cornerRadius, cornerRadius, paint);
        }
        if ((notRoundedCorners & CORNER_TOP_RIGHT) != 0) {
            canvas.drawRect(rectF.right - cornerRadius, 0, rectF.right, cornerRadius, paint);
        }
        if ((notRoundedCorners & CORNER_BOTTOM_LEFT) != 0) {
            canvas.drawRect(0, rectF.bottom - cornerRadius, cornerRadius, rectF.bottom, paint);
        }
        if ((notRoundedCorners & CORNER_BOTTOM_RIGHT) != 0) {
            canvas.drawRect(rectF.right - cornerRadius, rectF.bottom - cornerRadius, rectF.right, rectF.bottom, paint);
        }
        return result;
    }

    public String getId() {
        return getClass().getName() + Math.round(cornerRadius);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }

}
